<template>
  <lay-container :fluid="true" style="padding: 10px;padding-top: 10px;">
    <lay-row space="10">
      <lay-form :model="form">
        <lay-col :md="24">
          <lay-col :md="18">
            <lay-form-item label="标题" prop="title" required required-error-message="标题不能空">
              <lay-input v-model="form.title"></lay-input>
            </lay-form-item>
          </lay-col>
          <lay-col md="9">
            <lay-form-item label="来文文号" prop="docNumber">
              <lay-input v-model="form.docNumber"></lay-input>
            </lay-form-item>
          </lay-col>
          <lay-col md="9">
            <lay-form-item label="来文单位" prop="department">
              <lay-input v-model="form.department"></lay-input>
            </lay-form-item>
          </lay-col>
          <lay-col md="18">
            <lay-col md="6">
              <lay-form-item label="密级" prop="status">
                <lay-select v-model="form.miji" style="width:130px;">
                  <lay-select-option v-for="status in secretLevelDicts" :value="status.key" :label="status.value">
                  </lay-select-option>
                </lay-select>
              </lay-form-item>
            </lay-col>
          </lay-col>

          <lay-col :md="24">
          <lay-col md="10">
            <lay-form-item label="来文时间" prop="greatTime">
                <lay-date-picker v-model="form.greatTime" simple type="datetime"></lay-date-picker>
            </lay-form-item>
          </lay-col>
          <lay-col md="10">
            <lay-form-item label="经办人" prop="clercator">
              <lay-input v-model="form.clercator"></lay-input>
            </lay-form-item>
          </lay-col>
        </lay-col>

          <div v-permission="PerKey.Upload_Upload">
            <lay-field title="附件">
              <UploadAttments v-model="form.attments" :attList="getAttments" />
            </lay-field>
          </div>

          <lay-col md="18">
            <lay-form-item label="备注" prop="remarks">
              <lay-input v-model="form.remarks"></lay-input>
            </lay-form-item>
          </lay-col>

          <lay-col :md="24" class="saveBtn">
            <lay-button @click="submitClick" type="primary" v-permission="PerKey.DocReceived_AddOrEdit"
              prefix-icon="layui-icon-ok">
              {{ addoredittip }}
            </lay-button>
          </lay-col>
        </lay-col>
      </lay-form>
    </lay-row>

  </lay-container>
</template>
  
<script lang="ts" name="DocReceivedPost" setup>
import { reactive, ref, toRefs } from "vue";
import { layer } from "@layui/layer-vue";
import { postAdd, putEdit, getEdit } from "../../../../../api/module/oas/docreceived";
import { PerKey } from "../../../../../library/permissionConst"
import { DocReceived, DocCat } from '../../../../../types/doc'
import { Attment } from "../../../../../types/attment"
import Util from '../../../../../library/Util'
import UploadAttments from '../../../../../layouts/Upload/UploadAttments.vue'
interface Props {
  DocId: number,
}
const props = withDefaults(defineProps<Props>(), {
  DocId: 0,
});

const data = reactive({
  form: <DocReceived><unknown>[],
  getAttments: [<Attment>{}],
})

const { form, getAttments } = toRefs(data)

let addoredittip = ref();
const docCat = ref(<DocCat><unknown>[]);

const secretLevelDicts = ref()
secretLevelDicts.value = Util.dictfilter("Sys_SecretLevel")

getEdit(props.DocId).then((res) => {
  if (res.status == 1) {
    docCat.value = res.data.docCat;
    if (props.DocId == 0) {
      addoredittip.value = "新增";
      form.value = {
        id: 0,
        cid: 0,
        title: "",
        attments: "",
        miji: 1,
        uid: 0,
        greatTime: new Date(),
        docNumber: "",
        department: "",
        remarks: "",
        clercator: ""
      }
      getAttments.value = []
    } else {
      addoredittip.value = "修改";
      form.value = res.data.data
      getAttments.value = res.data.attments ?? []
    }
  } else {
    layer.msg("获取基本信息失败！", { icon: 2 })
  }
})

const submitClick = function () {
  if (!form.value.title || !form.value.docNumber) {
    layer.msg("标题和文号不能空")
    return
  }
  if (form.value.attments != null)//对附件进行类型转换
    form.value.attments = String(form.value.attments)
  if (form.value.id == 0) {
    postAdd(form.value).then((res) => {
      done(res)
    });
  } else {
    putEdit(form.value).then((res) => {
      done(res)
    });
  }
};

const emits = defineEmits(['update:dialogIsOk'])
const done = (res: any) => {
  if (res.status == 1) {
    layer.msg(res.msg, { time: 1000, icon: 1 }, () => {
      emits('update:dialogIsOk', 1)
    });
  } else {
    layer.msg(res.msg, { icon: 2 });
    return false;
  }
}

</script>
<style scoped>

</style>